package com.hdl.school.entity;

import com.hdl.school.bean.SerialHexCode;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class HexCodeQueue {
    CopyOnWriteArrayList<SerialHexCode> ctrlSerialCodes =new CopyOnWriteArrayList<>();
    CopyOnWriteArrayList<SerialHexCode> statusSerialCodes = new CopyOnWriteArrayList<>();

    public void clearCtrlSerialCode() {
        synchronized (ctrlSerialCodes) {
            ctrlSerialCodes.clear();
        }
    }

    public void addCtrlSerialCode(SerialHexCode serialHexCode) {
        if (serialHexCode == null) {
            return;
        }
        synchronized (ctrlSerialCodes) {
            if (ctrlSerialCodes != null && ctrlSerialCodes.size() > 0) {
                if (ctrlSerialCodes.size() > 40) {
                    ctrlSerialCodes.remove(0);
                }
                boolean added = false;
                for (int j = ctrlSerialCodes.size() - 1; j >= 0; j--) {
                    if (serialHexCode.getTime() > ctrlSerialCodes.get(j).getTime()) {
                        ctrlSerialCodes.add(j + 1, serialHexCode);
                        added = true;
                        break;
                    }
                }
                if (!added) {
                    ctrlSerialCodes.add(0, serialHexCode);
                }
            } else {
                ctrlSerialCodes.add(serialHexCode);
            }
        }
    }

    public SerialHexCode getFirstOveredCode() {
        long now = System.currentTimeMillis();
        synchronized (ctrlSerialCodes) {
            if (ctrlSerialCodes.size() > 0) {
                if (ctrlSerialCodes.get(0).getTime() <= now) {
                    SerialHexCode firstCode = ctrlSerialCodes.get(0);
                    removeFirstCtrlSerialCode();
                    return firstCode;
                }
            }
        }
        synchronized (statusSerialCodes) {
            if (statusSerialCodes.size() > 0) {
                if (statusSerialCodes.get(0).getTime() <= now) {
                    SerialHexCode firstCode = statusSerialCodes.get(0);
                    statusSerialCodes.remove(0);
                    return firstCode;
                }
            }
        }
        return null;
    }

    public void addStatusSerialCode(SerialHexCode serialHexCode) {
        if (serialHexCode == null) {
            return;
        }
        synchronized (statusSerialCodes) {
            if (statusSerialCodes.size() > 50) {
                statusSerialCodes.remove(0);
            }
            statusSerialCodes.add(serialHexCode);
        }
    }


    private void removeFirstCtrlSerialCode() {
        if (ctrlSerialCodes.size() > 0) {
            ctrlSerialCodes.remove(0);
        }
    }

    private void removeFirstStatusSerialCode() {
            if (statusSerialCodes.size() > 0) {
                statusSerialCodes.remove(0);
            }
    }

    public CopyOnWriteArrayList<SerialHexCode> getCtrlSerialCodes() {
        return ctrlSerialCodes;
    }

    public void setCtrlSerialCodes(CopyOnWriteArrayList<SerialHexCode> ctrlSerialCodes) {
        this.ctrlSerialCodes = ctrlSerialCodes;
    }

    public List<SerialHexCode> getStatusSerialCodes() {
        return statusSerialCodes;
    }

    public void setStatusSerialCodes(CopyOnWriteArrayList<SerialHexCode> statusSerialCodes) {
        this.statusSerialCodes = statusSerialCodes;
    }
}
